﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PolePosition.v3.Desktop.Editors.Controls
{
    public partial class XMissonPole : DialogEditBase
    {
        public XMissonPole()
        {
            InitializeComponent();

            BindCombos();
        }

        public Common.Enumerations.DataTypes DataType { get; private set; }

        private void BindCombos()
        {
            if (Program.LookupTableData != null)
            {
                ComboOptions.Kv(comboBoxKv);
                ComboOptions.PoleSpecies(comboBoxSpecies);
                ComboOptions.PoleXmissionClass(comboBoxClass);
                ComboOptions.PoleTreatment(comboBoxTreatment);
                ComboOptions.PoleXmissionTopTypes(comboBoxTopType);
            }

            if (Program.LoginResult != null && Program.LoginResult.Polygons != null)
                ComboOptions.Maps(comboBoxMap);
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object Data
        {
            get
            {
                return bindingSourcePole.DataSource;
            }
            set
            {
                bindingSourcePole.SuspendBinding();
                SetupDatabindings(value);
                bindingSourcePole.DataSource = value;
                bindingSourcePole.ResumeBinding();
            }
        }

        [DefaultValue(false)]
        private bool m_EditAllowed;
        public bool EditAllowed 
        {
            get 
            {
                return m_EditAllowed;
            }
            set
            {
                m_EditAllowed = value;

                this.textBoxSection.ReadOnly = !m_EditAllowed;
                this.textBoxLongitude.ReadOnly = !m_EditAllowed;
                this.textBoxLatitude.ReadOnly = !m_EditAllowed;
                this.textBoxYear.ReadOnly = !m_EditAllowed;
                this.textBoxLength.ReadOnly = !m_EditAllowed;
                this.textBoxLVisit.ReadOnly = !m_EditAllowed;
                this.comboBoxClass.Enabled = m_EditAllowed;
                this.comboBoxMap.Enabled = m_EditAllowed;
                this.comboBoxKv.Enabled = m_EditAllowed;
                this.comboBoxTreatment.Enabled = m_EditAllowed;
                this.comboBoxSpecies.Enabled = m_EditAllowed;
                this.comboBoxTopType.Enabled = m_EditAllowed;
            }
        }

        private void SetupDatabindings(object data)
        {

            if (data.GetType() == typeof(PolePosition.v3.EntityLibrary.VendorInspVisitXmissionStructuresPoleEntity))
            {
                DataType = PolePosition.v3.Common.Enumerations.DataTypes.ProjectsXmissionInspection;

                this.bindingSourcePole.DataSource = typeof(PolePosition.v3.EntityLibrary.VendorInspVisitXmissionStructuresPoleEntity);
                this.textBoxSection.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "SectionFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxMap.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "MapFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLongitude.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Longitude", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLatitude.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Latitude", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxKv.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "Vendor_Visit_TypeFK", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxYear.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Year", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxTreatment.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "TreatmentFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxClass.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "ClassFK", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLength.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Length", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxSpecies.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "SpecieFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxTopType.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "TopTypeFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLVisit.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "LVisitYear", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLVisitBy.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "LInspector", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxNumber.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Number", true, DataSourceUpdateMode.OnPropertyChanged));
            }
            else if (data.GetType() == typeof(PolePosition.v3.EntityLibrary.VendorVisitXMissionStructuresPoleEntity))
            {
                DataType = PolePosition.v3.Common.Enumerations.DataTypes.ProjectsXmissionGpsVisit;

                this.bindingSourcePole.DataSource = typeof(PolePosition.v3.EntityLibrary.VendorVisitXMissionStructuresPoleEntity);
                this.textBoxSection.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Section", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxMap.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "MapFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLongitude.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Longitude", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLatitude.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Latitude", true, DataSourceUpdateMode.OnPropertyChanged));
                //this.comboBoxKv.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "Vendor_Visit_TypeFK", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxYear.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Year", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxTreatment.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "XMissionStructures_PoleTreatmentFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxClass.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "XMissionStructures_PoleClassFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLength.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Length", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxSpecies.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "XMissionStructures_PoleSpeciesFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxTopType.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "XMissionStructures_PoleTopTypeFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLVisit.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "LVisit", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLVisitBy.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "LVisitor", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxNumber.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Number", true, DataSourceUpdateMode.OnPropertyChanged));
            }
            else if (data.GetType() == typeof(PolePosition.v3.EntityLibrary.UtilityAssetXMissionStructuresPoleEntity))
            {
                DataType = PolePosition.v3.Common.Enumerations.DataTypes.AssetsXmission;

                this.bindingSourcePole.DataSource = typeof(PolePosition.v3.EntityLibrary.VendorVisitXMissionStructuresPoleEntity);
                this.textBoxSection.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "SectionFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxMap.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "MapFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLongitude.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Longitude", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLatitude.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Latitude", true, DataSourceUpdateMode.OnPropertyChanged));
                //this.comboBoxKv.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "Vendor_Visit_TypeFK", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxYear.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Year", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxTreatment.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "XMissionStructures_PoleTreatmentFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxClass.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "XMissionStructures_PoleClassFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLength.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Length", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxSpecies.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "XMissionStructures_PoleSpeciesFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.comboBoxTopType.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bindingSourcePole, "XMissionStructures_PoleTopTypeFk", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLVisit.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "LVisit", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxLVisitBy.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "LVisitor", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxNumber.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourcePole, "Number", true, DataSourceUpdateMode.OnPropertyChanged));
            }            
        }
    }
}
